package cn.liutao.auth.api;

import cn.liutao.auth.entity.param.PayPwdParam;
import cn.liutao.auth.service.UserService;
import cn.liutao.common.entity.LoginUser;
import cn.liutao.common.utils.ServletUtils;
import cn.liutao.common.utils.ValidUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.groups.Default;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author: liutao
 * @description: 设置支付密码接口
 * @date: 2023/11/21 13:00
 */
@WebServlet("/user/payPwd")
public class PayPasswordServlet extends HttpServlet {
    private UserService userService;

    @Override
    public void init(){
        userService = UserService.getInstance();
    }

    /**
     * 校验用户是否设置支付密码
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer userId = LoginUser.getUserId();
        boolean flag = userService.checkPayPwd(userId);
        Map map = new LinkedHashMap();
        map.put("checked",flag);
        ServletUtils.ok(resp,map);

    }

    /**
     * 设置支付密码
     * @param req
     * @param resp
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp){
        PayPwdParam payPwdParam = ServletUtils.getBodyParam(req, PayPwdParam.class);
        ValidUtil.validated(payPwdParam, Default.class);
        Integer userId = LoginUser.getUserId();
        userService.setPayPwd(userId,payPwdParam);
        ServletUtils.ok(resp,"设置成功");
    }

    /**
     * 修改支付密码
     * @param req
     * @param resp
     */
    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp){
        PayPwdParam payPwdParam = ServletUtils.getBodyParam(req, PayPwdParam.class);
        ValidUtil.validated(payPwdParam, Default.class);
        Integer userId = LoginUser.getUserId();
        userService.updatePayPwd(userId,payPwdParam);
        ServletUtils.ok(resp,"修改成功");
    }

}
